(function($) {

	$.SJR = function(){};

	$.SJR.JSONENGINE = "1.0.0";

	var i_success = function(mainobject, subobject, type, response, status){
		if(type=='DELETE'){
			var o = {};
			for(var i in $(mainobject).values())
				o[i] = '';
			$(mainobject).values(o);
		}
		else{
			if(!$(mainobject).attr('class').match(/_jeClearForm/)){
				$(mainobject).values(response);
			} else {
				if(type!='GET'){
					var o = {};
					for(var i in $(mainobject).values())
						o[i] = '';
					$(mainobject).values(o);
				}
			}
		}
		var option_for_list = {};
		option_for_list.type = 'GET';
		option_for_list.url = '/_je/' + mainobject[0].id;
		var search = '#' + mainobject[0].id + '-search';
		var src = $(search).values();
		var cond = '';
//		for(var i in src){
//			if(src[i].trim()){
//				cond += '&cond=' + i + '.eq.' + src[i]; 
//			}
//		}
		cond += '&' + 'sort=_updatedAt.desc';
		option_for_list.data = cond;
		option_for_list.success = action_success(1, subobject, mainobject, 'GET');
		option_for_list.error = error;
		$.ajax(option_for_list);
	};

	var o_success = function(mainobject, subobject, type, response, status){
		mainobject.empty();
		var subobj = '#'+mainobject[0].id.slice(0,-5);
		for(var i in response){
			var div = $('<div/>');
			div.append(JSON.stringify(response[i]))
			div.click(function(){
				$(subobj).values(JSON.parse($(this).text()));
			});
			mainobject.append((div));
		}
		if($(subobject).attr('class').match(/_jeClearForm/)){
			var o = {};
			for(var i in $(subobject).values())
				o[i] = '';
			$(subobject).values(o);
		}
	};

	var action_success = function(isFn, mainobject, subobject, type){
		return function(response, status){
			if(isFn){
				o_success(mainobject, subobject, type, response, status);
			}
			else{
				i_success(mainobject, subobject, type, response, status);
			}
		}
	};

	var error = function(xhr, status, thrown){
		window.alert('error');
	};

	/**
	 * output: jsonengineから検索してきた一覧の表示先
	 * type: リクエストメソッド
	 * input: 入力フォーム
	 * input_success: 更新用$.ajax成功時のコールバック関数
	 */
	$.SJR.ajaxbase_beta = function(output, type, input, input_success){
		var option = {};
		var url = '';
		if(input[0]['_docId']){  //データ入力フォーム
			url = '/_je/' + input[0].id;
			var i = input.values();
			if(type=='POST'){
				if(i._docId.trim()==''){
					delete i._docId;
				}
			}
			option.type = type;
			if(type!='DELETE'){
				option.url = url;
				option.data = i;
			}else{
				option.url = url + '/' + i._docId;
			}
			option.success = action_success(0, input, output, type);
			option.error = error;
			$.ajax(option);
		}
		else {
			var url = '/_je/' + input[0].id.slice(0,-7);
			var src = input.values();
			var cond = '';
			for(var i in src){
				if(src[i].trim()){
					cond += '&cond=' + i + '.eq.' + src[i]; 
				}
			}
			if(cond=='') cond += '&' + 'sort=_updatedAt.desc';
			option.type = type;
			option.url = url;
			option.data = cond.substr(1,cond.length);
			option.success = action_success(1, output, input, type);
			option.error = error;
			$.ajax(option);
		}
	};

	$.fn.extend({
		_POST : function(input, input_success) {
			$.SJR.ajaxbase_beta(this, "POST", input, input_success);
			return this;
		},
		_GET : function(input, input_success) {
			$.SJR.ajaxbase_beta(this, "GET", input, input_success);
			return this;
		},
		_POST : function(input, input_success) {
			$.SJR.ajaxbase_beta(this, "POST", input, input_success);
			return this;
		},
		_PUT : function(input, input_success) {
			$.SJR.ajaxbase_beta(this, "PUT", input, input_success);
			return this;
		},
		_DELETE : function(input, input_success) {
			$.SJR.ajaxbase_beta(this, "DELETE", input, input_success);
			return this;
		}
	});

})(jQuery);

